﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Redis.Core
{
    /// <summary>
    /// Redis Key管理 和 String
    /// </summary>
    public class RedisKeys : RedisBase
    {
        public RedisKeys(Manager manager)
            : base(manager.GetClient())
        {
        }

        public RedisKeys()
            : base(Manager.Default.GetClient())
        {
        }


        public long AppendTo(String Key, String Item)
        {
            return Redis.AppendToValue(Key, Item);
        }


        public String SubString(String Key, int fromIndex, int toIndex)
        {
            return Redis.GetSubstring(Key, fromIndex, toIndex);
        }

        public Boolean Replace(String Key, String Item)
        {
            return Redis.Replace(Key, Item);
        }


        public IList<String> GetAllKeys()
        {
            return Redis.GetAllKeys();
        }


        public Int64 Decrement(String Key,UInt32 Amount)
        {
            return Redis.Decrement(Key, Amount);
        }


        public Int64 Increment(String Key, UInt32 Amount)
        {
            return Redis.Increment(Key, Amount);
        }


        public Boolean ExpireAt(string key, DateTime expireAt)
        {
            return Redis.ExpireEntryAt(key, expireAt);
        }


        public String GetRandomKey()
        {
            return Redis.GetRandomKey();
        }


        public TimeSpan GetTimeToLive(string key)
        {
            return Redis.GetTimeToLive(key);
        }

        public Boolean ExpireAt(string key, TimeSpan expireIn)
        {
            return Redis.ExpireEntryIn(key, expireIn);
        }

        public Boolean ExpireAt(string key, Int32 expireIn)
        {
            return Redis.ExpireEntryIn(key, new TimeSpan(0,0,0,0, expireIn));
        }


        public List<string> SearchKeys(string pattern)
        {
            return Redis.SearchKeys(pattern);
        }




        public void SetValues<T>(Dictionary<String, T> map)
        {
            Redis.SetAll(map);
        }

        
        public void SetValueIfNotExists(String Key, String Item)
        {
            Redis.SetEntryIfNotExists(Key, Item);
        }

        //public void SetValueIfNotExists<T>(String Key, T Item)
        //{
        //    Redis.SetEntryIfNotExists(Key, Item);
        //}

        public void SetValue<T>(String Key,T Item)
        {
            Redis.Set(Key, Item);
        }

        public T GetValue<T>(String Key)
        {
            return Redis.Get<T>(Key);
        }

        // Dictionary<string, T> GetValuesMap<T>(List<string> keys);

        
        public IDictionary<String, T> GetValues<T>()
        {
            return Redis.GetValuesMap<T>(Redis.GetAllKeys());
        }




        public IDictionary<String,T> GetValues<T>(params String [] keys)
        {
            return Redis.GetValuesMap<T>(keys.ToList());
        }


        public void Remove(IEnumerable<String> keys)
        {
            Redis.RemoveAll(keys);
        }

        public Boolean Remove(String key)
        {
            return Redis.Remove(key);
        }


        public Int64 Count
        {
            get
            {
                return Redis.GetSetCount("");
            }
        }

        public void Rename(String Key ,String newKey)
        {
            Redis.RenameKey(Key, newKey);
        }


        public Boolean IsExist(String Key)
        {
            return Redis.ContainsKey(Key);
        }

        public void Delete(String Key)
        {
            Redis.Remove(Key);
        }


    }
}

